package ch.bfh.fbi.mobiComp2013.sensornet.implementation;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ch.bfh.fbi.mobiComp2013.sensornet.definition.SensorCommunication;
import ch.bfh.fbi.mobiComp2013.sensornet.definition.SensorCommunicationManager;
import ch.bfh.fbi.mobiComp2013.sensornet.definition.SensorMessage;
import ch.bfh.fbi.mobiComp2013.sensornet.earthquake.EarthQuakeSensorMessage;

public class MySensorCommunication extends JFrame implements SensorCommunication {
	private SensorCommunicationManager chatMessageManager;
	
	private JButton sendButton;
	private JTextField sendEditText;
	private JScrollPane receiveTextScrollView;
	private JTextArea receiveTextView;
	private final String id;

	public MySensorCommunication() {
		this.id = "" + System.currentTimeMillis() % 100000;
		this.initGUIElements();
		this.initGUIListeners();
		this.setVisible(true);
	}

	

	private void initGUIElements() {
		this.setTitle(this.id);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.sendButton = new JButton("Send");
		this.sendEditText = new JTextField();
		this.receiveTextView = new JTextArea(20, 40);
		this.receiveTextView.setEditable(false);
		this.receiveTextScrollView = new JScrollPane();
		this.receiveTextScrollView.setViewportView(this.receiveTextView);
		this.getContentPane().add(this.receiveTextScrollView);
		final JPanel southPanel = new JPanel();
		southPanel.setLayout(new BorderLayout());
		southPanel.add(this.sendEditText);
		southPanel.add(this.sendButton, BorderLayout.EAST);
		this.getContentPane().add(southPanel, BorderLayout.SOUTH);
		this.pack();
	}

	/**
	 * @see SensorCommunication#onMessageReceived(SensorMessage)
	 * 
	 */
	@Override
	public void onMessageReceived(final SensorMessage chatMessage) {
		this.receiveTextView.append(chatMessage.getHeader() + ">"
				+ chatMessage.getBody() + "\n");
	}

	/**
	 * @see SensorCommunication#onMessageDelivered()
	 */
	@Override
	public synchronized void onMessageDelivered() {
		this.sendButton.setEnabled(true);

	}

	private void initGUIListeners() {
		final ActionListener listener = new OnSendButtonClick();
		this.sendButton.addActionListener(listener);
		this.sendEditText.addActionListener(listener);
		this.chatMessageManager = new MySensorManager(this);

	}

	class OnSendButtonClick implements ActionListener {

		@Override
		public void actionPerformed(final ActionEvent evt) {
			synchronized (MySensorCommunication.this) {

				//MySensorCommunication.this.chatMessageManager.send(new EarthQuakeSensorMessage(MySensorCommunication.this.id));
				MySensorCommunication.this.sendEditText.selectAll();
				MySensorCommunication.this.sendButton.setEnabled(false);
			}
		}
	}

	public static void main(final String[] args) {
		new MySensorCommunication();
	}
}